package org.zjkoye.crm.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.zjkoye.crm.entity.Workorder;
import org.zjkoye.crm.mapper.WorkorderMapper;
import org.zjkoye.crm.service.WorkorderService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class WorkorderServiceImpl extends ServiceImpl<WorkorderMapper, Workorder> implements WorkorderService {

    @Autowired
    private WorkorderMapper workorderMapper;

    @Override
    public IPage<Workorder> pageList(int pageSize, int currentPage, String status) {
        Page<Workorder> page = new Page<>(currentPage, pageSize);
        QueryWrapper<Workorder> queryWrapper = new QueryWrapper<>();
        if (status != null && !status.isEmpty()) {
            queryWrapper.eq("status", status);
        }
        return workorderMapper.selectPage(page, queryWrapper);
    }

    @Override
    public IPage<Workorder> getUnassignedWorkOrders(int pageSize, int currentPage) {
        Page<Workorder> page = new Page<>(currentPage, pageSize);
        QueryWrapper<Workorder> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("status", "未派单");
        return workorderMapper.selectPage(page, queryWrapper);
    }

    @Override
    public IPage<Workorder> getAssignedWorkOrders(int pageSize, int currentPage) {
        Page<Workorder> page = new Page<>(currentPage, pageSize);
        QueryWrapper<Workorder> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("status", "已派单");
        return workorderMapper.selectPage(page, queryWrapper);
    }

    @Override
    public List<String> getOperators() {
        QueryWrapper<Workorder> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("distinct operator");
        List<Workorder> workorders = workorderMapper.selectList(queryWrapper);
        return workorders.stream().map(Workorder::getOperator).collect(Collectors.toList());
    }
}
